import { MessageStyle } from "@/hooks/message";
import { createAxios, ZAxiosOption } from "@/utils/http";

const backendServiceGlobalOption: ZAxiosOption = {
  config: {
    baseURL: "http://localhost:3000",
    timeout: 0,
  },
  fieldConfig: {
    msgField: "msg",
  },
  carryToken: true,
};

const backendHttp = createAxios(backendServiceGlobalOption);

export const uploadChunks = (
  formData: any,
  onUploadProgress: any,
  signal: any
) =>
  backendHttp.request(
    {
      url: "upload",
      method: "post",
      headers: { "Content-Type": "multipart/form-data" },
      data: formData,
      onUploadProgress,
      signal,
    },
    {
      messageStyle: MessageStyle.none,
      retry: 2,
    }
  );

export const queryChunks = (params: { hash: string; fileName: string }) =>
  backendHttp.request<string[]>(
    {
      url: "upload/query",
      method: "get",
      params,
    },
    {
      messageStyle: MessageStyle.message,
    }
  );

export const mergeChunks = (params: { hash: string; fileName: string }) =>
  backendHttp.request<string[]>(
    {
      url: "upload/merge",
      method: "get",
      params,
    },
    {
      messageStyle: MessageStyle.message,
    }
  );
